#define MAX_MANTISA 3
 char *itoa( int i, char *buf );


/*
//char *ftoa2(float f, int *status) {
//	long mantissa, int_part, frac_part;
//	short exp2;
//	LF_t x;
//	char *p;
//	static char outbuf[15];
//	*status = 0;
//	if (f == 0.0){
//		outbuf[0] = '0';
//		outbuf[1] = '.';
//		outbuf[2] = '0';
//		outbuf[3] = 0;
//		return outbuf;
//		}
//	x.F = f;
//	exp2 = (unsigned char)(x.L >> 23) - 127;
//	mantissa = (x.L & 0xFFFFFF) | 0x800000;
//	frac_part = 0;
//	int_part = 0;
//	if (exp2 >= 31){
//		*status = 2; //too large
//		return 0;}
//	else if (exp2 < -23){
//		*status = 1; // too small
//		return 0;
//		}
//	else if (exp2 >= 23)
//		int_part = mantissa << (exp2 - 23);
//	else if (exp2 >= 0){
//		int_part = mantissa >> (23 - exp2);
//		frac_part = (mantissa << (exp2 + 1)) & 0xFFFFFF;
//		}
//	else /* if (exp2 < 0) */
//	frac_part = (mantissa & 0xFFFFFF) >> -(exp2 + 1);
//	p = outbuf;
//	
//	if (x.L < 0)
//	*p++ = '-';
//	if (int_part == 0) *p++ = '0';
//	else{
//		ltoa(p, int_part, 10);
//		while (*p) p++;
//		}
//	*p++ = '.';
//	if (frac_part == 0) *p++ = '0';
//	else{
//		char m, max;
//		max = sizeof (outbuf) - (p - outbuf) - 1;
//		if (max > 7) max = 7;
//		/* print BCD */
//		for (m = 0; m < max; m++){
//			/* frac_part *= 10; */
//			frac_part = (frac_part << 3) + (frac_part << 1);
//			*p++ = (frac_part >> 24) + '0';
//			frac_part &= 0xFFFFFF;
//			} 
//			/* delete ending zeroes */
//			for (--p; p[0] == '0' && p[-1] != '.'; --p);
//			++p;
//			}
//			*p = 0;
//			return outbuf;
//			}
//			
//
*/
/*
**************************************************
*
*    ftoa - converts float to string
*
***************************************************
*
*    This is a simple implemetation with rigid
*    parameters:
*            - Buffer must be 8 chars long
*            - 3 digits precision max
*            - absolute range is -524,287 to 524,287 
*            - resolution (epsilon) is 0.125 and
*              always rounds down
**************************************************/
void ftoa(float Value, char* Buffer)
{
   union
   {
       float f;
   
       struct
       {
           unsigned int    mantissa_lo : 16;
           unsigned int    mantissa_hi : 7;    
           unsigned int     exponent : 8;
           unsigned int     sign : 1;
       };
   } helper;
   
   unsigned long mantissa;
   signed char exponent;
   unsigned int int_part;
   char frac_part[3];
   int i, count = 0;
   
   helper.f = Value;
   //mantissa is LS 23 bits
   mantissa = helper.mantissa_lo;
   mantissa += ((unsigned long) helper.mantissa_hi << 16);
   //add the 24th bit to get 1.mmmm^eeee format
   mantissa += 0x00800000;
   //exponent is biased by 127
   exponent = (signed char) helper.exponent - 127;
   
   //too big to shove into 8 chars
   if (exponent > 18)
   {
       Buffer[0] = 'I';
       Buffer[1] = 'n';
       Buffer[2] = 'f';
       Buffer[3] = '\0';
       return;
   }
   
   //too small to resolve (resolution of 1/8)
   if (exponent < -3)
   {
       Buffer[0] = '0';
       Buffer[1] = '\0';
       return;
   }
   
   count = 0;
   
   //add negative sign (if applicable)
   if (helper.sign)
   {
       Buffer[0] = '-';
       count++;
   }
   
   //get the integer part
   int_part = mantissa >> (23 - exponent);    
   //convert to string
   itoa(int_part, &Buffer[count]);
   
   //find the end of the integer
   for (i = 0; i < 8; i++)
       if (Buffer[i] == '\0')
       {
           count = i;
           break;
       }        

   //not enough room in the buffer for the frac part    
   if (count > 5)
       return;
   
   //add the decimal point    
   Buffer[count++] = '.';
   
   //use switch to resolve the fractional part
   switch (0x7 & (mantissa  >> (20 - exponent)))
   {
       case 0:
           frac_part[0] = '0';
           frac_part[1] = '0';
           frac_part[2] = '0';
           break;
       case 1:
           frac_part[0] = '1';
           frac_part[1] = '2';
           frac_part[2] = '5';            
           break;
       case 2:
           frac_part[0] = '2';
           frac_part[1] = '5';
           frac_part[2] = '0';            
           break;
       case 3:
           frac_part[0] = '3';
           frac_part[1] = '7';
           frac_part[2] = '5';            
           break;
       case 4:
           frac_part[0] = '5';
           frac_part[1] = '0';
           frac_part[2] = '0';            
           break;
       case 5:
           frac_part[0] = '6';
           frac_part[1] = '2';
           frac_part[2] = '5';            
           break;
       case 6:
           frac_part[0] = '7';
           frac_part[1] = '5';
           frac_part[2] = '0';            
           break;
       case 7:
           frac_part[0] = '8';
           frac_part[1] = '7';
           frac_part[2] = '5';                    
           break;
   }
   
   //add the fractional part to the output string
   for (i = 0; i < 3; i++)
       if (count < 7)
           Buffer[count++] = frac_part[i];
   
   //make sure the output is terminated
   Buffer[count] = '\0';
}


 char *itoa( int i, char *buf )
{
    char *p = buf;
   
   *p++ = '0' + (i/1000);
   *p++ = '0' + ((i%1000)/100);
   *p++ = '0' + ((i%100)/10);
   *p++     = '0' + (i%10);      
   *p = '\0';
    return p;
} 


